package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);



    }

    @Override
    public void add(Role role, Integer[] perm, Integer[] menu) {
        roleDao.add(role);

        //添加角色关联的权限

        for (Integer permID : perm) {
            Map<String, Integer> map = new HashMap<>();
            map.put("role_id",role.getId());
            map.put("permission_id",permID);
            roleDao.addCheckRoleAndPermission(map);
        }

        //添加角色关联的菜单
        for (Integer memuID : menu) {
            Map<String,Integer> map = new HashMap<>();
            map.put("role_id",role.getId());
            map.put("menu_id",memuID);
            roleDao.addCheckRoleAndMenu(map);

        }
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdByPermissionId(Integer id) {

        return roleDao.findAllPermission(id);
    }

    @Override
    public List<Integer> findRoleIdByMenuId(Integer id) {

        return roleDao.findRoleIdByMenuId(id);
    }

    @Override
    public void edit(Role role, Integer[] perm, Integer[] menu) {
        roleDao.edit(role);
        roleDao.delPerm(role.getId());
        roleDao.delMenu(role.getId());
        //添加角色关联的权限

        for (Integer permID : perm) {
            Map<String, Integer> map = new HashMap<>();
            map.put("role_id",role.getId());
            map.put("permission_id",permID);
            roleDao.addCheckRoleAndPermission(map);
        }

        //添加角色关联的菜单
        for (Integer memuID : menu) {
            Map<String,Integer> map = new HashMap<>();
            map.put("role_id",role.getId());
            map.put("menu_id",memuID);
            roleDao.addCheckRoleAndMenu(map);

        }
    }

    @Override
    public void delById(Integer id) {
        roleDao.delPerm(id);
        roleDao.delMenu(id);
        roleDao.delRole(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
